package com.wzxy.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.wzxy.model.Department;
import com.wzxy.model.Employee;
import com.wzxy.service.IDepartmentService;
import com.wzxy.service.IEmployeeService;


@Controller
@RequestMapping("/employee")
public class EmployeeController {
	
	@Resource
	IEmployeeService employeeService;
	@Resource
	IDepartmentService departmentService;
	
	/**
	 * 显示员工列表
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/list")
	public String list(Model model){
		List<Employee> list = employeeService.list("from Employee", null);
		model.addAttribute("list", list);
		return "employee_list";
	}
	
	@RequestMapping(value = "/department/list")
	public String findAllDepartment(Model model){
		List<Department> list = departmentService.list("from Department", null);
		model.addAttribute("list", list);
		return "add_employee";
	}
	
	/**
	 * 添加员工
	 * @param employee
	 * @return
	 */
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public String sava(Employee employee){	

		//employee.setEno(AutoCode.getAutoCode()); //自动生成员工编号		
		employeeService.add(employee);
		return "redirect:/employee/list";
	}
	
	/**
	 * 跳转到更新页面，并把需要更新的数据在更新页面显示
	 * @param eid
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/toUpdate",method = RequestMethod.GET)
	public String toUpdate(String eid,Model model){
		Employee employee = employeeService.load(Integer.valueOf(eid));
		model.addAttribute("eid", employee.getEid());
		model.addAttribute("eno", employee.getEno());
		model.addAttribute("ename",employee.getEname());
		model.addAttribute("sex", employee.getSex());
		model.addAttribute("joinDate", employee.getJoinDate());
		model.addAttribute("birthday", employee.getBirthday());
		List<Department> list = departmentService.list("from Department", null);
		model.addAttribute("list", list);
		//model.addAttribute("dname", employee.getDname());
		return "update_employee";
	}
	
	/**
	 * 更新一条记录
	 * @param department
	 * @return
	 */
	@RequestMapping(value = "/update",method = RequestMethod.POST)
	public String update(Employee employee){
		employeeService.update(employee);
		return "redirect:/employee/list";
	}
	
	/**
	 * 删除一条记录
	 * @param employee
	 * @return
	 */
	@RequestMapping(value = "/delete",method = RequestMethod.GET)
	public String delete(Employee employee){
		employeeService.delete(employee.getEid());
		return "redirect:/employee/list";
	}
	
	/**
	 * 根据员工名字查询员工信息
	 * @param ename
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/findByName",method = RequestMethod.POST)
	public String findByName(String ename,Model model){
		String hql = "from Employee where ename = '"+ename+"'";
		List<Employee> list = employeeService.list(hql, null);
		model.addAttribute("list", list);
		return "employee_list";
	}
}
